<template>
  <BasicDrawer 
    v-bind="$attrs" 
    @register="registerDrawer" 
    title="编辑字段" 
    width="580px" 
    @ok="handleSubmit" 
    showFooter
    destroyOnClose
  >
    <MyAlert type="success">
      <template #message>
        <div>
          <div>1、当操作符合为等于、包含等过滤符号时候, 如果不满足条件, 则本次消息推送将不会被通知</div>
          <div>2、当操作符合为赋值时, 指定的过滤值会被赋值到数据中</div>
          <div>2、当操作过滤时候且字段类型为数组, 则数组中必须满足所有条件才会被推送, 只要有一条数据不满足则忽略本次推送</div>
        </div>
      </template>
    </MyAlert>
    <BasicForm @register="registerForm"/>
  </BasicDrawer>
</template>
<script lang="ts" setup>
  import MyAlert from "/@/components/My/MyAlert.vue"
  import { ref, h, computed, unref, useAttrs } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { fieldFormSchemas } from '../data';
  import { MsgFieldType } from '../../topic/data';
  import { BasicDrawer, useDrawerInner} from '/@/components/Drawer';
  import * as evbApi from '/@/api/infra/eventBus';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { ColEx } from '/@/components/Form/src/types';
  const { createMessage } = useMessage();

  const emit = defineEmits(['success', 'register']);
  const attrs = useAttrs()
  const record = ref<any>({});

  const [registerForm, {setProps, resetFields, setFieldsValue, validate, updateSchema}] = useForm({
    labelWidth: 90,
    schemas: fieldFormSchemas,
    showActionButtonGroup: false,
    wrapperCol: { lg: 24, md: 24 },
  });
  
  const [registerDrawer, {setDrawerProps, closeDrawer}] = useDrawerInner(async (data) => {
    await resetFields();
    record.value = data.record || {}
    console.log('record:', record.value)
    setDrawerProps({ confirmLoading: false, loading: true })
    try {
      await setFieldsValue({ 
        ...record.value
      })
    } finally {
      setDrawerProps({ loading: false})
    }
  });

  async function handleSubmit() {
    try {
      let values = await validate();
      setDrawerProps({confirmLoading: true});

      await evbApi.updateSubscribeField({...values});
      closeDrawer();
      createMessage.success(`更新成功`)
      emit('success');
    } finally {
      setDrawerProps({confirmLoading: false});
    }
  }
</script>
